<?php

class Ms_Model_Category extends Zend_Db_Table_Abstract
{
    protected $_name = "core_categories";
    
    public function getCategories($where = array(), $order = null)
    {
        $select = $this->select();
        if (count($where) > 0){
            foreach ($where as $key=>$value){
                $select->where($key.'= ?', $value);
            }
        }
        if ($order){
            $select->order($order);
        }
        $result = $this->fetchAll($select);
        if ($result){
            return $result;
        }
        else{
            return null;
        }
    }
    
    public function getFirstFidById($id){
        $select = $this->select();
        $select->where('id = ?', $id);
        return $this->getFirstFidByFid($this->fetchAll($select)->current()->fid);
    }
    
    public function getFirstFidByFid($fid){
        $select = $this->select();
        $select->where('id = ?', $fid);
        $result = $this->fetchAll($select)->current();
        if ($result->fid) {
            $this->getFirstFidByFid($fid);
        } else {
            return $fid;
        }
    }
    
    public function getTableName() {
        return $this->_name;
    }
}

